//
// Created by DengLibin on 2019/1/24 0024.
//
#include <iostream>
#include <map>
using namespace std;
struct TreeLinkNode {
  int val;
  TreeLinkNode *left, *right, *next;
  TreeLinkNode(int x) : val(x), left(NULL), right(NULL), next(NULL) {}
};
//层级-该层级当前节点 映射
map<int, TreeLinkNode *> m;
void connect(int level,TreeLinkNode *root) {
    if(root == NULL){
        return;
    }
    // cout << level<< "->"<<root->val<<endl;
    map<int,TreeLinkNode *>:: iterator it = m.find(level);
    if(it == m.end()){
        m.insert(make_pair(level, root));
    }else{
        // cout << it->second->val << "->"<<root->val<<endl;
        it->second->next = root;
        m.erase(level);
        m.insert(make_pair(level,root));
    }
    connect(level+1,root->left);
    connect(level+1,root->right);
}
/**
 * 115 填充同一层的兄弟节点
 * @param root
 */

void connect(TreeLinkNode *root){
    connect(0,root);
}

int main77(){
    TreeLinkNode node1 = {1};
    TreeLinkNode node2 = {2};
    TreeLinkNode node3 = {3};
    node1.left = &node2;
    node1.right = &node3;
    TreeLinkNode node4 = {4};
    TreeLinkNode node5 = {5};
    node2.left = &node4;
    node2.right = &node5;
    TreeLinkNode node6 = {6};
    TreeLinkNode node7 = {7};
    node3.left = &node6;
    node3.right = &node7;
    connect(1,&node1);
    return 0;
}

